﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Aga.Controls.Tree;
using OpenComposite.Workflow;
using OpenComposite.EII.Repository;

namespace OpenComposite.EII.Designer.Workflow.Forms
{
	public partial class FormColumnMapper : Form
	{
		public FormColumnMapper(ColumnMapperActivity activity)
		{
			InitializeComponent();
			_act = activity;
		}

		private ColumnMapperActivity _act = null;

		private void FormColumnMapper_Load(object sender, EventArgs e)
		{
			TreeModel model = new TreeModel();
#if DEBUG
			//MappingItemNode node = new MappingItemNode();
			//node.Name = "ItemID";
			//node.Type = "int";
			//model.Nodes.Add(node);
#endif
			if ( _act == null ) return;
			LogicalActivity logAct = Global.Data.RepositoryService.GetItem<LogicalActivity>(_act.LogicalActivityId);
			if ( logAct == null ) return;
			foreach ( BusinessObject bo in logAct.GetSubscribedBusinessObjects().BusinessObjects ) {
				MappingItemNode node = new MappingItemNode();
				node.Name = bo.Name;
				node.Type = bo.Name;
				foreach ( BusinessObjectField field in bo.BusinessObjectFields ) {
					createTreeNodes(node, field);
				}
				model.Nodes.Add(node);
			}
			advTreeInput.Model = model;
			advTreeInput.ExpandAll();

			if ( logAct.OutputBusinessObject == null ) return;
			TreeModel outputModel = new TreeModel();
			MappingItemNode outNode = new MappingItemNode();
			outNode.Name = logAct.OutputBusinessObject.Name;
			outNode.Type = logAct.OutputBusinessObject.Name;
			foreach ( BusinessObjectField field in logAct.OutputBusinessObject.BusinessObjectFields ) {
					createTreeNodes(outNode, field);
			}
			outputModel.Nodes.Add(outNode);
			advTreeOutput.Model = outputModel;
			advTreeOutput.ExpandAll();
		}

		private void createTreeNodes(MappingItemNode node, BusinessObjectField field)
		{
			MappingItemNode child = new MappingItemNode();
			child.Name = field.Name;
			if ( field.BusinessObject != null ) {
				child.Type = field.BusinessObject.Name;
				foreach ( BusinessObjectField subField in field.BusinessObject.BusinessObjectFields ) {
						createTreeNodes(child, subField);
				}
			} else if ( field.Noun != null ) {
				child.Type = field.Noun.DataType;
			}
			node.Nodes.Add(child);
		}
	}

	internal class MappingItemNode:Node
	{
		public string Name { get; set; }
		public string Type { get; set; }
		public string DisplayName
		{
			get
			{
				return string.Format("{0} ({1})", this.Name, this.Type);
			}
		}
		public object UserData { get; set; }
		public BusinessObject BusinessObject
		{
			get { return this.UserData as BusinessObject; }
			set { this.UserData = value; }
		}
		public Noun Noun
		{
			get { return this.UserData as Noun; }
			set { this.UserData = value; }
		}
		public bool IsNoun
		{
			get { return UserData is Noun; }
		}
	}
}